package com.rock.upms.client.shiro.session;

import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisManager {

	private String host = "127.0.0.1";
	private int port = 6379;
	private int expire = 0;
	private int timeout = 0;
	private String password = "123456";
	private static JedisPool jedisPool = null;

	public void init() {
		if (jedisPool == null) {
			if ((this.password != null) && (!"".equals(this.password))) {
				jedisPool = new JedisPool(new JedisPoolConfig(), this.host, this.port, this.timeout, this.password);
			} else if (this.timeout != 0) {
				jedisPool = new JedisPool(new JedisPoolConfig(), this.host, this.port, this.timeout);
			} else {
				jedisPool = new JedisPool(new JedisPoolConfig(), this.host, this.port);
			}
		}
	}

	public byte[] get(byte[] key) {
		byte[] value = null;
		Jedis jedis = jedisPool.getResource();
		try {
			value = jedis.get(key);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	public byte[] set(byte[] key, byte[] value) {
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.set(key, value);
			if (this.expire != 0) {
				jedis.expire(key, this.expire);
			}
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	public byte[] set(byte[] key, byte[] value, int expire) {
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.set(key, value);
			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	public void expire(String[] keys, int expire) {
		Jedis jedis = jedisPool.getResource();
		try {
			if (keys != null) {
				for (String key : keys) {
					jedis.expire(key, expire);
				}
			}
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/* Error */
	public void del(byte[] key) {
		// Byte code:
		// 0: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 3: invokevirtual 18 redis/clients/jedis/JedisPool:getResource
		// ()Lredis/clients/jedis/Jedis;
		// 6: astore_2
		// 7: aload_2
		// 8: aload_1
		// 9: invokevirtual 24 redis/clients/jedis/Jedis:del
		// ([B)Ljava/lang/Long;
		// 12: pop
		// 13: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 16: aload_2
		// 17: invokevirtual 20 redis/clients/jedis/JedisPool:returnResource
		// (Lredis/clients/jedis/Jedis;)V
		// 20: goto +13 -> 33
		// 23: astore_3
		// 24: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 27: aload_2
		// 28: invokevirtual 20 redis/clients/jedis/JedisPool:returnResource
		// (Lredis/clients/jedis/Jedis;)V
		// 31: aload_3
		// 32: athrow
		// 33: return
		// Line number table:
		// Java source line #126 -> byte code offset #0
		// Java source line #128 -> byte code offset #7
		// Java source line #130 -> byte code offset #13
		// Java source line #131 -> byte code offset #20
		// Java source line #130 -> byte code offset #23
		// Java source line #132 -> byte code offset #33
		// Local variable table:
		// start length slot name signature
		// 0 34 0 this RedisManager
		// 0 34 1 key byte[]
		// 6 22 2 jedis Jedis
		// 23 9 3 localObject Object
		// Exception table:
		// from to target type
		// 7 13 23 finally
	}

	/* Error */
	public void flushDB() {
		// Byte code:
		// 0: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 3: invokevirtual 18 redis/clients/jedis/JedisPool:getResource
		// ()Lredis/clients/jedis/Jedis;
		// 6: astore_1
		// 7: aload_1
		// 8: invokevirtual 25 redis/clients/jedis/Jedis:flushDB
		// ()Ljava/lang/String;
		// 11: pop
		// 12: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 15: aload_1
		// 16: invokevirtual 20 redis/clients/jedis/JedisPool:returnResource
		// (Lredis/clients/jedis/Jedis;)V
		// 19: goto +13 -> 32
		// 22: astore_2
		// 23: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 26: aload_1
		// 27: invokevirtual 20 redis/clients/jedis/JedisPool:returnResource
		// (Lredis/clients/jedis/Jedis;)V
		// 30: aload_2
		// 31: athrow
		// 32: return
		// Line number table:
		// Java source line #138 -> byte code offset #0
		// Java source line #140 -> byte code offset #7
		// Java source line #142 -> byte code offset #12
		// Java source line #143 -> byte code offset #19
		// Java source line #142 -> byte code offset #22
		// Java source line #144 -> byte code offset #32
		// Local variable table:
		// start length slot name signature
		// 0 33 0 this RedisManager
		// 6 21 1 jedis Jedis
		// 22 9 2 localObject Object
		// Exception table:
		// from to target type
		// 7 12 22 finally
		
	}

	/* Error */
	public Long dbSize() {
		// Byte code:
		// 0: lconst_0
		// 1: invokestatic 26 java/lang/Long:valueOf (J)Ljava/lang/Long;
		// 4: astore_1
		// 5: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 8: invokevirtual 18 redis/clients/jedis/JedisPool:getResource
		// ()Lredis/clients/jedis/Jedis;
		// 11: astore_2
		// 12: aload_2
		// 13: invokevirtual 27 redis/clients/jedis/Jedis:dbSize
		// ()Ljava/lang/Long;
		// 16: astore_1
		// 17: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 20: aload_2
		// 21: invokevirtual 20 redis/clients/jedis/JedisPool:returnResource
		// (Lredis/clients/jedis/Jedis;)V
		// 24: goto +13 -> 37
		// 27: astore_3
		// 28: getstatic 9
		// cn/chinapost/jdpt/core/web/common/security/shiro/redis/RedisManager:jedisPool
		// Lredis/clients/jedis/JedisPool;
		// 31: aload_2
		// 32: invokevirtual 20 redis/clients/jedis/JedisPool:returnResource
		// (Lredis/clients/jedis/Jedis;)V
		// 35: aload_3
		// 36: athrow
		// 37: aload_1
		// 38: areturn
		// Line number table:
		// Java source line #150 -> byte code offset #0
		// Java source line #151 -> byte code offset #5
		// Java source line #153 -> byte code offset #12
		// Java source line #155 -> byte code offset #17
		// Java source line #156 -> byte code offset #24
		// Java source line #155 -> byte code offset #27
		// Java source line #157 -> byte code offset #37
		// Local variable table:
		// start length slot name signature
		// 0 39 0 this RedisManager
		// 4 34 1 dbSize Long
		// 11 21 2 jedis Jedis
		// 27 9 3 localObject Object
		// Exception table:
		// from to target type
		// 12 17 27 finally
		return null;
	}

	public Set<byte[]> keys(String pattern) {
		Set<byte[]> keys = null;
		Jedis jedis = jedisPool.getResource();
		try {
			keys = jedis.keys(pattern.getBytes());
		} finally {
			jedisPool.returnResource(jedis);
		}
		return keys;
	}

	public String getHost() {
		return this.host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return this.port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getExpire() {
		return this.expire;
	}

	public void setExpire(int expire) {
		this.expire = expire;
	}

	public int getTimeout() {
		return this.timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
